import 'package:flutter/material.dart';
import 'dart:math';

class CirclePainter extends CustomPainter {
  
  double currentProgress;

  CirclePainter(this.currentProgress);

  @override
  void paint(Canvas canvas, Size size) {
    Paint outCircle = Paint()
      ..strokeWidth = 10
      ..color = Colors.black
      ..style = PaintingStyle.stroke;

    Paint compleArc = Paint()
      ..strokeWidth = 10
      ..color = Colors.redAccent
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round;

    Offset center = Offset(size.width / 2, size.height / 2);
    double raduis = min(size.width / 2, size.height / 2) - 10;

    canvas.drawCircle(center, raduis, outCircle);

    double angle = 2 * pi * (currentProgress / 100);
    canvas.drawArc(Rect.fromCircle(center: center,radius: raduis), -pi / 2, angle, false, compleArc);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }
  
}

class CircelProgress extends StatefulWidget {
  @override
  _CircelProgressState createState() => _CircelProgressState();
}

class _CircelProgressState extends State<CircelProgress> with SingleTickerProviderStateMixin {

  AnimationController progressContorller;
  Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    progressContorller = AnimationController(vsync: this, duration: Duration(milliseconds: 1000));
    CurvedAnimation curve = CurvedAnimation(parent: progressContorller, curve: Curves.bounceInOut);
    animation = Tween<double>(begin: 0, end: 80).animate(curve)..addListener((){
      setState(() {
        
      });
    });

    progressContorller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("进度动画"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: (){
            Navigator.of(context).pop();
          },
        ),
      ),
      body: Container(
        height: 200,
        width: 200,
        child: CustomPaint(
          painter: CirclePainter(animation.value),
          child: IconButton(
            icon: Icon(Icons.cached),
            onPressed: (){
              progressContorller.forward();
            },
          ),
        ),
      ),
    );
  }
}